import request from "@/api/request";

/**
 * 获取菜单分页数据
 * @param params
 * @returns
 */
export const getSysMenusList = (params: SysMenuListParams) => {
  return new Promise<SysMenuEntityType[]>(async (resolve, reject) => {
    const res: AxiosResData<SysMenuEntityType[]> = await request.get(
      "/sysMenu/menuList",
      { params }
    );
    resolve(res.data?.result || []);
  });
};

/**
 * 新增菜单数据
 * @param data
 * @returns
 */
export const addSysMenus = (data: SysMenuEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.post("/sysMenu/add", data);
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 修改菜单数据
 * @param data
 * @returns
 */
export const updateSysMenus = (data: SysMenuEntityType) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.put("/sysMenu/update", data);
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 删除菜单数据-根据ID
 * @param id
 * @returns
 */
export const deleteSysMenusById = (id: string) => {
  return new Promise<boolean>(async (resolve, reject) => {
    const res: AxiosResData<boolean> = await request.delete(
      `/sysMenu/deleteById/${id}`
    );
    if (res.data?.code === 200) {
      resolve(res.data?.result);
    }
  });
};

/**
 * 获取菜单下拉树
 * @param params
 * @returns
 */
export const getSysMenusSelectTree = () => {
  return new Promise<SysMenuEntityType[]>(async (resolve, reject) => {
    const res: AxiosResData<SysMenuEntityType[]> = await request.get(
      "/sysMenu/menuSelectTree"
    );
    resolve(res.data?.result || []);
  });
};
